#!/usr/bin/perl

use Cwd;

$baseurl = getcwd;

($YEAR, $MONTH, $DAY, $HOUR, $MIN, $SEC) = (localtime)[5, 4, 3, 2, 1, 0];
$YEAR += 1900;
$now = "$MONTH/$DAY/$YEAR $HOUR:$MIN:$SEC";


#@args = split(" ", <STDIN>);
#
#if ($args[0] eq 'CREATING') {
#    $mode = 'CREATING';
#} else {
#    $mode = 'UPDATING';
#}
#
#if ($args[1] =~ /\//) {
#    $baseurl = $args[1];
#}

if ($ARGV[0] eq 'CREATING') {
    $mode = 'CREATING';
} else {
    $mode = 'UPDATING';
}

if ($ARGV[1] =~ /\//) {
    ($baseurl = $ARGV[1]) =~ s/[\/]*$//;
}

open (STDERR, "> $baseurl/errors_Runs.txt");
open (STDOUT, "> $baseurl/prints_Runs.txt");

# local system variables
$connect = $baseurl . '/connect.pl';
require $connect;

use IO::File;

$pid_file = "/tmp/run_MULtifacet_scripts.pid";

unless (-e $pid_file) {

$pid_file = IO::File->new("> /tmp/run_MULtifacet_scripts.pid") || die "cannot open $pid_file for writing: $!\n";
binmode ($pid_file, ':utf8');
flock($pid_file, 2);

    print $pid_file $$ . "\n";

    $pid_file->autoflush(1);
    close($pid_file, 2) || "cannot close $pid_file: $!";

$pid_file = "/tmp/run_MULtifacet_scripts.pid";

($HOUR, $MIN, $SEC) = (localtime)[2, 1, 0];
$start = "$HOUR:$MIN:$SEC";

open (MYFILE, "> $baseurl/outputting.txt");
print MYFILE "$start\n";
close(MYFILE);

$a = 1;

if(($HOUR =~ /19/) || ($mode eq 'CREATING')) {
@expect_scripts_array = qw(items orders checkins tocs_970s courses bibs authorities);
@perl_scripts_array = qw(universal_ids_load tocs_970s_XML_parser bibs_load checkins_load orders_load items_load courses_load authorities_load);
} else {
@expect_scripts_array = qw(items orders checkins courses bibs authorities);
@perl_scripts_array = qw(universal_ids_load bibs_load checkins_load orders_load items_load courses_load authorities_load);
}

if(($HOUR =~ /23/) && ($mode eq 'UPDATING')) {
    @delete_scripts = qw(items_deleted checkins_deleted bibs_deleted orders_deleted courses_deleted authorities_deleted);
    push (@expect_scripts_array, @delete_scripts);
    push (@perl_scripts_array, @delete_scripts);
}

foreach $expect_scripts(@expect_scripts_array) {

$script = $baseurl . '/' . $expect_scripts . '.exp ' . $mode . ' ' . $baseurl;

print 'script: ' . $script . "\n";

($YEAR, $MONTH, $DAY, $HOUR, $MIN, $SEC) = (localtime)[5, 4, 3, 2, 1, 0];
$YEAR += 1900;
$now = "$MONTH/$DAY/$YEAR $HOUR:$MIN:$SEC";

open (MYFILE, ">> $baseurl/outputting.txt");
print MYFILE "$a. $expect_scripts.exp \nStarted at: \t$now\n";
close (MYFILE);

#system($script);

$OUTPUT = `$script`;
undef($OUTPUT);

($YEAR, $MONTH, $DAY, $HOUR, $MIN, $SEC) = (localtime)[5, 4, 3, 2, 1, 0];
$YEAR += 1900;
$then = "$MONTH/$DAY/$YEAR $HOUR:$MIN:$SEC";

open (MYFILE, ">> $baseurl/outputting.txt");
print MYFILE "Ended at: \t$then\n\n";
close (MYFILE);

$a++;

}

$script_times = '';

open (MYFILE, "< $baseurl/outputting.txt");
while ( <MYFILE> ) { 
    $script_times .= $_; 
}
close (MYFILE);

if($consortium ne '') {
    push (@perl_scripts_array, consortium_load);
}

foreach $perl_scripts(@perl_scripts_array) {

$script = $baseurl . '/' . $perl_scripts . '.pl ' . $mode . ' ' . $baseurl;

print 'script: ' . $script . "\n";

($YEAR, $MONTH, $DAY, $HOUR, $MIN, $SEC) = (localtime)[5, 4, 3, 2, 1, 0];
$YEAR += 1900;
$now = "$MONTH/$DAY/$YEAR $HOUR:$MIN:$SEC";

$OUTPUT = `$script`;
undef($OUTPUT);

($YEAR, $MONTH, $DAY, $HOUR, $MIN, $SEC) = (localtime)[5, 4, 3, 2, 1, 0];
$YEAR += 1900;
$then = "$MONTH/$DAY/$YEAR $HOUR:$MIN:$SEC";

$script_times .= $a . '.' . $perl_scripts . '.pl ' . "\nStarted at: \t$now\nEnded at: \t$then\n\n";

$a++;

}

($YEAR, $MONTH, $DAY, $HOUR, $MIN, $SEC) = (localtime)[5, 4, 3, 2, 1, 0];
$YEAR += 1900;
$now = "$MONTH/$DAY/$YEAR $HOUR:$MIN:$SEC";

# added by casson to load into solr
#system('/home/rcasson/solr/apache-solr-1.1.0-incubating/allitems_master/load.php');

($YEAR, $MONTH, $DAY, $HOUR, $MIN, $SEC) = (localtime)[5, 4, 3, 2, 1, 0];
$YEAR += 1900;
$then = "$MONTH/$DAY/$YEAR $HOUR:$MIN:$SEC";

$script_times .= $a . ". allitems_master/load.php\nStarted at: \t$now\nEnded at: \t$then\n\n";

($HOUR, $MIN, $SEC) = (localtime)[2, 1, 0];
$end = "$HOUR:$MIN:$SEC";

open(MAIL, "|/usr/sbin/sendmail -t");

## Mail Header
print MAIL "To: $email_address\n";
print MAIL "From: root@localhost\n";
print MAIL "Subject: run_MULtifacet_updated_scripts.pl has finished $start -- $end\n\n";
## Mail Body
print MAIL "run_MULtifacet_updated_scripts.pl _dev has finished\r\n Starttime: $start \tEndtime: $end\r\n\r\n$script_times\r\n";

close(MAIL);

    unlink($pid_file) || die "cannot unlink $pid_file: $!\n";

} else {

open(MAIL, "|/usr/sbin/sendmail -t");

## Mail Header
print MAIL "To: $email_address\n";
print MAIL "From: root@localhost\n";
print MAIL "Subject: run_MULtifacet_updated_scripts.pl did not run $start -- $end\n\n";
## Mail Body
print MAIL "detected that the previous run_MULtifacet_scripts.pl hadn't finished.\r\ncould mean it's hung-up, could be nothing, but might want to check, and you'll need to remove " . $pid_file . " before it runs again\r\n";

close(MAIL);

}

